<?php

/**
 * This is the model class for table "competency_unit".
 *
 * The followings are the available columns in table 'competency_unit':
 * @property string $COMP_UNIT_CODE
 * @property string $COMP_UNIT_NAME
 * @property string $COMP_UNIT_DESC
 * @property integer $USER_CREATED
 * @property string $TIME_CREATED
 * @property integer $USER_UPDATED
 * @property string $TIME_UPDATED
 *
 * The followings are the available model relations:
 * @property CompetencyElement[] $competencyElements
 */
class CompetencyUnit extends CMasterModel
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return CompetencyUnit the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'competency_unit';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('USER_CREATED, USER_UPDATED', 'numerical', 'integerOnly'=>true),
			array('COMP_UNIT_CODE', 'length', 'max'=>10),
			array('COMP_UNIT_NAME', 'length', 'max'=>200),
			array('COMP_UNIT_DESC', 'length', 'max'=>1000),
			array('TIME_CREATED, TIME_UPDATED', 'safe'),
			array('COMP_UNIT_CODE, COMP_UNIT_NAME, COMP_UNIT_DESC', 'required'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('COMP_UNIT_CODE, COMP_UNIT_NAME, COMP_UNIT_DESC, USER_CREATED, TIME_CREATED, USER_UPDATED, TIME_UPDATED', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'competencyElements' => array(self::HAS_MANY, 'CompetencyElement', 'COMP_UNIT_CODE')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'COMP_UNIT_CODE' => 'Comp Unit Code',
			'COMP_UNIT_NAME' => 'Comp Unit Name',
			'COMP_UNIT_DESC' => 'Comp Unit Desc',
			'USER_CREATED' => 'User Created',
			'TIME_CREATED' => 'Time Created',
			'USER_UPDATED' => 'User Updated',
			'TIME_UPDATED' => 'Time Updated',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('COMP_UNIT_CODE',$this->COMP_UNIT_CODE,true);
		$criteria->compare('COMP_UNIT_NAME',$this->COMP_UNIT_NAME,true);
		$criteria->compare('COMP_UNIT_DESC',$this->COMP_UNIT_DESC,true);
		/*$criteria->compare('USER_CREATED',$this->USER_CREATED);
		$criteria->compare('TIME_CREATED',$this->TIME_CREATED,true);
		$criteria->compare('USER_UPDATED',$this->USER_UPDATED);
		$criteria->compare('TIME_UPDATED',$this->TIME_UPDATED,true);*/

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function getElementId($id)
	{       
		$criteria=new CDbCriteria;// only select the 'title' column
		$criteria->condition='COMP_UNIT_CODE=:COMP_UNIT_CODE';
		$criteria->params=array(':COMP_UNIT_CODE'=>$id);

		$element=new CActiveDataProvider('CompetencyElement', array(
		    'pagination'=>array(
			'pageSize'=>5,
		    ),
		    'criteria'=>$criteria,
		));
		
		return $element;
	}
	
	
}
